Eg.Timer = Eg.Class.extend({
  target: 0,
  base: 0,
  last: 0,

  init: function (seconds) {
    this.base = Eg.Timer.time;
    this.last = Eg.Timer.time;

    this.target = seconds || 0;
  },


  set: function (seconds) {
    this.target = seconds || 0;
    this.base = Eg.Timer.time;
  },


  reset: function () {
    this.base = Eg.Timer.time;
  },


  tick: function () {
    var delta = Eg.Timer.time - this.last;
    this.last = Eg.Timer.time;
    return delta;
  },


  delta: function () {
    return Eg.Timer.time - this.base - this.target;
  }
});

Eg.Timer._last = 0;
Eg.Timer.time = 0;
Eg.Timer.timeScale = 1;
Eg.Timer.maxStep = 0.05;

Eg.Timer.step = function () {
  var current = Date.now();
  var delta = (current - Eg.Timer._last) / 1000;
  Eg.Timer.time += Math.min(delta, Eg.Timer.maxStep) * Eg.Timer.timeScale;
  Eg.Timer._last = current;
};